// pages/order/order-manage/order-manage.js
const controllers = require('../../controllers/controllerOrder.js').controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cloudOrderNum: 0,
    goodsOrderNum: 0,
    serverOrderNum: 0,
    cloudCommission: 0,
    orderTotalNum: 0,
    orderTotalPrice: 0,
    orderCost: 0,
    staffCommission: 0,
    cloudTotalNum: 0,
    cloudTotalPrice: 0,
    totalProfit: 0,
    list: [],
    nl: '',
    staffall: 0,
    staffsevice: 0,
    staffproduct: 0,
    staffcloud: 0,
    staffordercom: 0,
    staffsevicecom: 0,
    staffproductcom: 0,
    staffcloudcom: 0,
    orderProfit:0,
    goodsAgreeNum:""
  },
  //跳转到服务订单列表页面
  toSeviceOrder(e) {
    wx.navigateTo({
      url: '/pages/order/service-order/service-order?id=' + e.currentTarget.id
    })
  },
  tabChange(e) {
    let index = e.detail.index;
    console.log(index)


    if (wx.getStorageSync('diantype') == 1) {
      if (index == 0) {
        wx.redirectTo({
          url: '/pages/Index/Index'
        })
      } else
      if (index == 1) {
        wx.redirectTo({
          url: '/pages/order/order-manage/order-manage'
        })
      } else if (index == 2) {
        wx.redirectTo({
          url: '/pages/my/index'
        })
      } else {
        wx.redirectTo({
          url: '/pages/my/index'
        })
      }
    } else {
      if (index == 0) {
        wx.redirectTo({
          url: '/pages/order/order-manage/order-manage'
        })
      } else
      if (index == 1) {
        wx.redirectTo({
          url: '/pages/my/index'
        })
      }
    }
  },
  //跳转到商品订单列表页面
  toProductOrder(e) {
    wx.navigateTo({
      url: '/pages/order/product-order/product-order?id=' + e.currentTarget.id
    })
  },

  //跳转到云商品订单列表页面
  toCloudOrder(e) {
    wx.navigateTo({
      url: '/pages/order/cloud-order/cloud-order?id=' + e.currentTarget.id
    })
  },

  //跳转到代客预约界面
  toDaiAppointmen() {
    wx.navigateTo({
      url: '/pages/order/dai-appointment/dai-appointment',
    })
  },

  //跳转到预约记录列表页面
  toAppointmentRecord() {
    wx.navigateTo({
      url: '/pages/order/appointment-record/appointment-record',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('diantype') == 1) {
      this.setData({
        list: app.globalData.list,
        nl: 1
      })
    } else {
      this.setData({
        list: app.globalData.list02,
        nl: 0
      })
    }

    //获取店铺员工列表
    controllers.getSales({
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      console.log(res);
      wx.setStorageSync('sales', res.data)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    //获取订单数据
    controllers.orderCount({
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      console.log(res);
      that.setData({
        cloudOrderNum: res.data.cloudOrderNum,
        goodsOrderNum: res.data.goodsOrderNum,
        serverOrderNum: res.data.serverOrderNum,
        cloudCommission: res.data.cloudCommission,
        orderTotalNum: res.data.orderTotalNum,
        orderTotalPrice: res.data.orderTotalPrice,
        orderCost: res.data.orderCost,
        staffCommission: res.data.staffCommission,
        cloudTotalNum: res.data.cloudTotalNum,
        cloudTotalPrice: res.data.cloudTotalPrice,
        goodsAgreeNum:res.data.goodsAgreeNum
      })
      this.setData({
        'serveState.state2':'',
        'serveState.state7':'',
        'serveState.state1':'',
        'goodsState.state2':'',
        'goodsState.state1':'',
        'cloudState.state7':'',
        'cloudState.state11':'',
        'cloudState.state8':'',
        goodsAgreeNum:''
      })
      res.data.orderList.forEach(function(item,index){
        that.setData({
          serveState:{},
          goodsState:{},
          cloudState:{}
        })
        if(item.goodsType==0){
          if(item.state==2){
            that.setData({
              'serveState.state2':item.num  //待预约
            })
          }else if(item.state==7){
            that.setData({
              'serveState.state7':item.num  //待服务
            })
          }else if(item.state==1){
            that.setData({
              'serveState.state1':item.num  //拼团中
            })
          }else if(item.state==1){
            that.setData({
              'serveState.state1':item.num  //待审核
            })
          }

        }else if(item.goodsType==1){
          if(item.state==2){
            that.setData({
              'goodsState.state2':item.num  //待取货
            })
          }else if(item.state==1){
            that.setData({
              'goodsState.state1':item.num  //拼团中
            })
          }

        }else{
          if(item.state==7){
            that.setData({
              'cloudState.state7':item.num  //通知取货
            })
          }else if(item.state==11){
            that.setData({
              'cloudState.state11':item.num  //待取货
            })
          }else if(item.state==8){
            that.setData({
              'cloudState.state8':item.num  //待评价
            })
          }

        }
      }),
      //获取员工详情
      controllers.staffIndex({
        shopId: wx.getStorageSync('shopId')
      }).then(res => {
        console.log(res);
        that.setData({
          staffall: (res.data.cloudOrderNum||0) + (res.data.goodsOrderNum||0) + (res.data.serverOrderNum||0),
          staffcloud: res.data.cloudOrderNum||0,
          staffproduct: res.data.goodsOrderNum||0,
          staffsevice: res.data.serverOrderNum||0,
          staffordercom: (res.data.cloudOrderCommission + res.data.goodsOrderCommission + res.data.serverOrderCommission)||0,
          staffsevicecom: res.data.serverOrderCommission||0,
          staffproductcom: res.data.goodsOrderCommission||0,
          staffcloudcom: res.data.cloudOrderCommission||0,
        })

        that.setData({
          orderProfit:parseFloat((that.data.orderTotalPrice-that.data.orderCost-that.data.staffCommission+that.data.staffcloudcom)||0).toFixed(2)
        })
        wx.setStorageSync('shopAccountId', res.data.id)
      })
    })

    //获取店铺首页信息
    controllers.shopInfo({
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      console.log(res);
      that.setData({
        totalProfit: res.data.totalProfit
      })
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})